<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog_m extends CI_Model {

    /**
     * Insert a new record into the database,
     * Returns the insert ID.
     *
     * @param array $data Information
     * @return integer
     */
    public function insert_object($objParams = array(), $extendParams = array())
    {
        //Insert object to DB
        $this->db->insert('object', $objParams);
        
        //Get insert_id
        $objID = $this->db->insert_id();
        
        //If success
        if($objID)
        {
            //Set object_id
            $extendParams['object_id'] = $objID;
            
            //Insert object_extend to DB
            $this->db->insert('object_extend', $extendParams);
        }
        
        //Return insert_id
        return $objID;
    }
    
    /**
     * Update a record by ID 
     * Returns boolean result.
     *
     * @param array $objParams, $extendParams
     * @return boolean
     */
    public function update_object($id, $objParams = array(), $extendParams = array())
    {
        //Update object to DB
        $result = $this->db->where('id', $id)
                            ->set($objParams)
                            ->update('object');
        
        if($result && !empty($extendParams)){
            //Update object to DB
            $result = $this->db->where('object_id', $id)
                            ->set($extendParams)
                            ->update('object_extend');
        }
        
        //Return result
        return $result;
    }
    
    /**
     * Delete object by id
     * @param type $id 
     * @return boolean
     */
    public function delete_object($id)
    {
        //Check id
        if(empty($id)){
            return FALSE;
        }
        
        //Delete object
        $result = $this->db->delete('object', array('id' => $id));
        
        //Delete object_extend
        if($result){
            $this->db->delete('object_extend', array('object_id' => $id));
        }
        
        //return result
        return $result;
    }
    
    /**
     * Similar to get(), but returns a result array of
     * many result objects.
     *
     * @param string $key The key to search by
     * @param string $val The value of that key
     * @return array
     * @author Phil Sturgeon
     */
    public function get_object_by($params)
    {
        return $this->db->where($params)
                ->order_by('created_on', 'desc')
                ->get('object')
                ->result_array();
    }

    /**
     * Searches blog posts based on supplied data array
     * @param $data array
     * @return array
     */
    public function search($data = array())
    {
        if (array_key_exists('category_id', $data))
        {
                $this->db->where('category_id', $data['category_id']);
        }

        if (array_key_exists('status', $data))
        {
                $this->db->where('status', $data['status']);
        }

        if (array_key_exists('keywords', $data))
        {
                $matches = array();
                if (strstr($data['keywords'], '%'))
                {
                        preg_match_all('/%.*?%/i', $data['keywords'], $matches);
                }

                if (!empty($matches[0]))
                {
                        foreach ($matches[0] as $match)
                        {
                                $phrases[] = str_replace('%', '', $match);
                        }
                }
                else
                {
                        $temp_phrases = explode(' ', $data['keywords']);
                        foreach ($temp_phrases as $phrase)
                        {
                                $phrases[] = str_replace('%', '', $phrase);
                        }
                }

                $counter = 0;
                foreach ($phrases as $phrase)
                {
                        if ($counter == 0)
                        {
                                $this->db->like('blog.title', $phrase);
                        }
                        else
                        {
                                $this->db->or_like('blog.title', $phrase);
                        }

                        $this->db->or_like('blog.body', $phrase);
                        $this->db->or_like('blog.intro', $phrase);
                        $this->db->or_like('profiles.display_name', $phrase);
                        $counter++;
                }
        }
        return $this->get_all();
    }
    
    /**
     * Get object detail by $id
     * @param int $id
     * @return array 
     */
    public function get_object_detail($id)
    {
        return $this->db->select('*')->join('object_extend', 'object_id = object.id', 'left')
                                  ->where(array('object.id' => $id))
				  ->get('object')
				  ->row_array();
    }
    
    /**
     * Get object where in
     * @param type $key
     * @param type $params
     * @return type 
     */
    public function get_object_in($key, $params)
    {
        return $this->db->where_in($key, $params)
                ->order_by('created_on', 'desc')
                ->get('object')
                ->result_array();
    }

}